<?php

/**
 * Simple and unique functions without classification
 */


/**
 * Password hash
 *
 * @param string $passwd
 * @return string hash
 */
function hashPassword($passwd)
{
    return md5($passwd);
}

/**
 * Turns a block (2d-array) by 90°, diff calculation with another block
 *
 * @param array $newer_block
 * @param array $older_block
 * @param string $index
 * @return array 2d turned
 */
function formatBlock(array $newer_block, array $older_block = array(), $index = 'id') //opposite of array_map(null,$arr1,$arr2)
{
    $uarray = array();
    foreach ($newer_block as $ind => $array) {
        $diff_array = !empty($older_block[$ind]) ? array_diff_assoc($array, $older_block[$ind]) : $array;
        foreach ($diff_array as $key => $val) {
            if (!isset($uarray[$key])) {
                $uarray[$key] = array();
            }
            if (array_key_exists($index, $array)) {
                $uarray[$key][$array[$index]] = $val;
            } else {
                $uarray[$key][$ind] = $val;
            }
        }
    }
    return $uarray;
}

/**
 * Comparing two multidimensional arrays
 *
 * @param array $newer_array
 * @param array $older_array
 * @param array $write_rights
 * @param bool $check_write_rights
 * @return array diff
 */
function array_multi_diff( array $newer_array, array $older_array, array $write_rights = array(), $check_write_rights = false)
{
    $relevant = array();
    foreach ($newer_array as $ind => $array)
    {
        if (!is_array($array) || !is_array($older_array[$ind]))
        {
            trigger_error("array_multi_diff inputs have to be didimensional arrays", E_PARSE);
        }
        $relevant[$ind] = array_diff_assoc($array, $older_array[$ind]);
    }
    if ($check_write_rights)
    {
        $relevant = array_intersect_key($relevant, $write_rights);
    }
    return $relevant;
}

function getLang($filename, $extension = '.mo')
{
	return core::getLang($filename, $extension);
}


// --[ Functions by peberos ]------------------------------------------------------------------------------------------
function pretty_number($n,$floor=true)
{
	if($floor){$n = floor($n);}
	return number_format($n,0,",",".");
}
function includeLang($filename,$ext='.mo'){
	global $lang;

	// added if to test availability of the file by Phlegma
	if (!file_exists(UGAMELA_ROOT_PATH."lang/".DEFAULT_LANG.'/'.$filename.$ext)) return false;

	include(UGAMELA_ROOT_PATH."lang/".DEFAULT_LANG.'/'.$filename.$ext);
	return true;
}
// --[ Simple Templates by peberos ]--------------------------------------------------------------------------------------
function parsetemplate($template, $array)
{
	return preg_replace('#\{([a-z0-9\-_]*?)\}#Ssie', '( ( isset($array[\'\1\']) ) ? $array[\'\1\'] : \'\' );', $template);
}
function gettemplate($templatename){

	$filename =  UGAMELA_ROOT_PATH . TEMPLATE_DIR . TEMPLATE_NAME . '/' . $templatename . ".tpl";
	return @file_get_contents($filename);

}
// --[/ END Functions by peberos ]-----------------------------------------------------------------------------------